unit counterparties;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, DBGrids,
  Buttons, Grids;

type

  { TfrmCounterParties }

  TfrmCounterParties = class(TForm)
    btnAddGroup: TBitBtn;
    btnCreate: TBitBtn;
    btnDelete: TBitBtn;
    btnMove: TBitBtn;
    DBCounterParties: TDBGrid;
    procedure btnAddGroupClick(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
    procedure btnDeleteClick(Sender: TObject);
    procedure btnMoveClick(Sender: TObject);
    procedure DBCounterPartiesDblClick(Sender: TObject);
    procedure DBCounterPartiesDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure GetGroupTitle;
  private
    { private declarations }
  public
    { public declarations }
    CurrentGroupRecordNumber:Integer;
  end;

var
  frmCounterParties: TfrmCounterParties;
  LastRecNo:String;
  groupTitle:String;
implementation

{$R *.lfm}
uses main,groupselect,ZAbstractRODataset,counterpartiescreateedit,counterpartiescreateeditgroup;

{ TfrmCounterParties }

{ TODO 3 : Контрагенти - переміщення/додавання }

procedure TfrmCounterParties.GetGroupTitle;
begin
  groupTitle:='';
  if (CurrentGroupRecordNumber<>0)  then begin
     FrmMain.ZQuery.Active:=False;
     FrmMain.ZQuery.SQL.Text:='SELECT title FROM counterparties WHERE id='+IntToStr(CurrentGroupRecordNumber);
     FrmMain.ZQuery.Active:=True;
     groupTitle:=FrmMain.DSQuery.DataSet.FieldByName('title').AsString;
  end;
  frmCounterPartiesCreateEdit.edtGroup.Text:=groupTitle;
  FrmCounterPartiesCreateEditGroup.edtGroup.Text:=groupTitle;
  FrmMain.ZQuery.Active:=False;
end;

procedure TfrmCounterParties.FormCreate(Sender: TObject);
begin
  FrmMain.ZTCounterParties.Filtered:=false;
  FrmMain.ZTCounterParties.SortedFields:='title,isgroup';
  FrmMain.ZTCounterParties.SortType:=stDescending;
  FrmMain.ZTCounterParties.Filter:='parentid=0';
  FrmMain.ZTCounterParties.Filtered:=true;
  CurrentGroupRecordNumber:=0;
end;

procedure TfrmCounterParties.FormShow(Sender: TObject);
begin
  frmMain.ZTCounterParties.Refresh;
end;

procedure TfrmCounterParties.DBCounterPartiesDrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
bmp: TBitmap;
begin
    if (Column.FieldName = 'id') and (DBCounterParties.DataSource.DataSet.FieldByName('isgroup').AsBoolean) then begin
       bmp := TBitmap.Create;
      if (Column.Field.AsString=LastRecNo) then begin
        FrmMain.ImageList2.GetBitmap(3,bmp);
      end else begin
        FrmMain.ImageList2.GetBitmap(0,bmp);
      end;
       DBCounterParties.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end else if (Column.FieldName = 'id') then begin
       bmp := TBitmap.Create;
       FrmMain.ImageList2.GetBitmap(2,bmp);
       DBCounterParties.Canvas.Draw(Rect.Left,Rect.Top,bmp);
       bmp.Free;
    end;
end;

procedure TfrmCounterParties.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  DBCounterParties.DataSource.DataSet.Refresh;
end;

procedure TfrmCounterParties.DBCounterPartiesDblClick(Sender: TObject);
var
  id,parentid:String;
  group:Boolean;
begin
  id:=DBCounterParties.DataSource.DataSet.FieldByName('id').AsString;
  parentid:=DBCounterParties.DataSource.DataSet.FieldByName('parentid').AsString;
  group:=DBCounterParties.DataSource.DataSet.FieldByName('isgroup').AsBoolean;
  if (group)  AND (LastRecNo=id) then begin
    FrmMain.ZTCounterParties.Filtered:=false;
    FrmMain.ZTCounterParties.SortedFields:='isgroup';
    FrmMain.ZTCounterParties.SortType:=stDescending;
    FrmMain.ZTCounterParties.Filter:='id='+parentid+' or parentid='+parentid;
    FrmMain.ZTCounterParties.Filtered:=true;
    LastRecNo:=parentid;
    CurrentGroupRecordNumber:=StrToInt(parentid);
  end else begin
  if ((group) AND (parentid<>id))
  then begin
    FrmMain.ZTCounterParties.Filtered:=false;
    FrmMain.ZTCounterParties.SortedFields:='isgroup';
    FrmMain.ZTCounterParties.SortType:=stDescending;
    FrmMain.ZTCounterParties.Filter:='id='+id+' or parentid='+id;
    FrmMain.ZTCounterParties.Filtered:=true;
    LastRecNo:=id;
    CurrentGroupRecordNumber:=StrToInt(id);
  end;
  end;
  if (group=False) then begin
    GetGroupTitle;
    frmCounterPartiesCreateEdit.ShowModal;
  end;
end;

procedure TfrmCounterParties.btnCreateClick(Sender: TObject);
begin
  GetGroupTitle;
  FrmMain.DSCounterParties.DataSet.Append;
  frmCounterPartiesCreateEdit.CurrentGroupRecordNumber:=CurrentGroupRecordNumber;
  frmCounterPartiesCreateEdit.ShowModal;
end;

procedure TfrmCounterParties.btnDeleteClick(Sender: TObject);
var
  res:Integer;
begin
  res:=MessageDlg('','Переконайтесь що запис ніде не використовується, Видалити запис?',mtConfirmation,mbYesNo,0);
  if (res=mrYes) then begin
    frmMain.execQuery('DELETE FROM counterparties WHERE id='+DBCounterParties.DataSource.DataSet.FieldByName('id').AsString);
    frmMain.history('Контрагенти','Видалення '+DBCounterParties.DataSource.DataSet.FieldByName('title').AsString);
    FrmMain.DSCounterParties.DataSet.Refresh;
  end;
end;

procedure TfrmCounterParties.btnMoveClick(Sender: TObject);
begin
  if (DBCounterParties.DataSource.DataSet.FieldByName('isgroup').AsBoolean) then begin
    CurrentGroupRecordNumber:=DBCounterParties.DataSource.DataSet.FieldByName('id').AsInteger;
    frmGroupSelect.groupsId:=2;
    frmGroupSelect.ShowModal;
    if (frmGroupSelect.selectedGroupId<>EmptyStr) then begin
      frmMain.execQuery('UPDATE counterparties SET parentid='+frmGroupSelect.selectedGroupId+' WHERE id='+IntToStr(CurrentGroupRecordNumber));
      frmMain.DSCounterParties.DataSet.Refresh;
    end;
  end else begin
    CurrentGroupRecordNumber:=DBCounterParties.DataSource.DataSet.FieldByName('id').AsInteger;
    frmGroupSelect.groupsId:=2;
    frmGroupSelect.ShowModal;
    if (frmGroupSelect.selectedGroupId<>EmptyStr) then begin
      frmMain.execQuery('UPDATE counterparties SET parentid='+frmGroupSelect.selectedGroupId+' WHERE id='+DBCounterParties.DataSource.DataSet.FieldByName('id').AsString);
      frmMain.DSCounterParties.DataSet.Refresh;
    end;
  end;
end;

procedure TfrmCounterParties.btnAddGroupClick(Sender: TObject);
begin
  GetGroupTitle;
  FrmMain.DSCounterPartiesGroup.DataSet.Append;
  frmCounterPartiesCreateEditGroup.ShowModal;
end;

end.

